<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" href="../../css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" href="../../css/ztree/demo.css" type="text/css">
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/common.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<input type="hidden" id="roleId" name="roleId">
			<!-- <input type="hidden" id="orgId" name="orgId" value="1"> -->
			<div class="form-group">
				<label class="col-md-2 control-label">角色名</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="角色" type="text" name="roleName" id="roleName"
						data-bv-notempty="true"
						data-bv-notempty-message="角色 不能为空">
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-2 control-label'>归属机构</label>
				<div class='col-md-10'>
					<input class='form-control' type='text' name='orgId' id='orgId' onclick="openIframeOrgList(this)" readonly="readonly" placeholder='点击选择'>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">描述</label>
				<div class="col-md-10">
					<textarea class="form-control" placeholder="描述" name="description"  id="description"></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">权限</label>
				<div class="col-md-10">
					<ul id="treeDemo" class="ztree"></ul>
				</div>
			</div>

			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" onclick="location.href='roleList.html'">返回</button>
						<button class="btn btn-primary" type="submit" onclick="add()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</form>
</div>

	<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript" src="../../js/my/ztree-menu.js"></script>
	<script type="text/javascript">
	layui.use('layer', function(){
	    var layer = layui.layer;
	});
	
		$.fn.zTree.init($("#treeDemo"), getSettting(), getMenuTree());
		
		initData();
		
		function initData(){
			var id = GetQueryString("id");

			if(id){
				$.ajax({
					type : 'get',
					url : urlRootPrefix + '/roles/'+id,
					async : false,
					success : function(data) {
						var data = data.data;
						$("#roleId").val(data.roleId);
						$("#roleName").val(data.roleName);
						$("#description").val(data.description);
						$('#orgId').val(data.orgName);
						$('#orgId').attr("orgId",data.orgId);
					}
				});
				
				initMenuDatas(id);
			}
		}
		
		
		$('#form').bootstrapValidator();
		
		function add() {
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
		    if(!bootstrapValidator.isValid()){
			   return;
		    }
		    
		    var formdata = $("#form").serializeObject();
		    formdata.permissionIds = getCheckedMenuIds();
		    formdata.orgId = $("#orgId").attr("orgId");
			$.ajax({
				type : 'post',
				url : urlRootPrefix + '/roles',
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(data) {
					if(!data.success){
						layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
						$("button[type='submit']").removeAttr("disabled");
						return;
					}
					layer.msg("成功", {shift: -1, time: 1000}, function(){
                        location.href = "roleList.html";
                    });
				}
			});
		}
		function openIframeOrgList(f) {
			var id = GetQueryString("id");
			if(id){
				return;
			}
			var dt = {'ids':$(f).attr("followOrgId"),'names':$(f).val()};
			layer.open({
				type : 2,
				title : "选择归属业务员",
				shadeClose : false,
				shade : 0.8,
				maxmin : false, //开启最大化最小化按钮
				area : [ '800px', '650px' ],
				content : urlRootPrefix + "/pages/common/orgList.html",
				success: function ( layero,index) {
		        	//找到它的子窗口的body
		            var body = layer.getChildFrame('body', index), iframeWin = window[layero.find('iframe')[0]['name']];  
		          //为子窗口元素赋值
		            body.contents().find("#ids").val(dt.ids);
		            body.contents().find("#names").val(dt.names);
		            //调用子窗口的事件
		            iframeWin.initData();
		            iframeWin.init();
		        },
				btn : [ '确定', '取消' ],
				yes : function(index, layero) {
					//按钮【按钮一】的回调
					var body = layer.getChildFrame('body', index);
					$("#orgId").attr("orgId",body.contents().find("#ids").val());
					$("#orgId").val(body.contents().find("#names").val());
					initRoles($("#orgId").attr("orgId"));
					layer.close(index);
				}
			});
		}
	</script>
</body>
</html>